package cn.italkcloud.cph.constants.enums.vos;

import cn.italkcloud.cph.utils.DateUtil;
import lombok.Getter;

import java.util.Date;

/**
 * @author dominate
 * @since 2025/3/10
 **/
@Getter
public enum SqlEnum {

    CHECK_CONNECTION("SELECT 1"),
    SELECT_GATEWAY_FEE_REPORT("select * from e_reportgatewaymappingfee_%d where customeraccount in( %p )"),
    SELECT_CDR_QUERY_BY_PHONE("select " +
            "id,callere164,calleraccesse164,callergatewayid,callertogatewaye164,callerip,callerrtpip,calleee164," +
            "calleeaccesse164,calleegatewayid,calleetogatewaye164,calleeip,starttime,stoptime,holdtime,feetime,fee," +
            "endreason,enddirection,agentaccount,agentname,customeraccount,customername" +
            " from e_cdr_%d where calleeaccesse164 = '%p'"),
    SELECT_CDR_REPORT("select " +
            "id,callergatewayid,calleegatewayid,starttime,stoptime,holdtime,feetime,fee,endreason,enddirection,agentaccount," +
            "agentname,customeraccount,customername" +
            " from e_cdr_%d where id > '%p' and starttime between '%p' and '%p'");;
    final String sql;

    SqlEnum(String sql) {
        this.sql = sql;
    }

    private static String createTableDate(Date startDate, int days) {
        if (days == 0) {
            return DateUtil.dateToFormatStr(startDate, DateUtil.DATA_FORMAT_DAY);
        }
        Date targetDate = DateUtil.getDateBeforeOrAfterDays(startDate, days);
        return DateUtil.dateToFormatStr(targetDate, DateUtil.DATA_FORMAT_DAY);
    }

    public String prepareSql(Date startDate, int days, String... params) {
        String targetSql = this.sql.replaceFirst("%d", createTableDate(startDate, days));
        for (String param : params) {
            targetSql = targetSql.replaceFirst("%p", param);
        }
        return targetSql;
    }

    public String prepareSql(Date startDate, int days, int index, int size, String... params) {
        String targetSql = this.prepareSql(startDate, days, params);
        return targetSql + " limit " + index + "," + size;
    }


}
